package servlet;

import bean.LoginInformation;
import bean.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 2:59 下午 2021/5/27
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String userName = request.getParameter("userName");
        System.out.println("获取到的用户名为：" + userName);
        String password = request.getParameter("password");
        System.out.println("获取到的密码为：" + password);
        UserService userService=new UserService();
        boolean isLogin=userService.loginService(new LoginInformation(userName,password));
        System.out.println(isLogin);
        if(isLogin==true){
            System.out.println("登录成功，欢迎使用！");
            // 将登录成功的用户信息放入session对象中实现多个请求共享
            request.getSession().setAttribute("loginInformation",new LoginInformation(userName,password));
            System.out.println(request.getSession().getAttribute("loginInformation"));

            response.sendRedirect("main?page=0");
        }else{
            response.getWriter().write("登陆失败，请检查用户名和密码！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
